<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 提现申请管理
 *
 * @icon fa fa-circle-o
 */
class Tixian extends Backend
{

    /**
     * Tixian模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Tixian');
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);

        if (!$row)
            $this->error(__('No Results were found'));

        $data = $row->toArray();

        unset($data['user_id']);
        unset($data['createtime']);
        unset($data['updatetime']);
        // unset($data['status']);

        $this->view->assign("row", $data);
        return $this->view->fetch();
    }

    //提现审核
    public function handle(){
        $id = $this->request->request('id');
        $status = $this->request->request('status');
        $res = $this->model->where(['id' => $id])->update(['status' => $status,'updatetime' => time()]);

        if($res !== false){

            $tixian = $this->model->get($id);
            if($status == '1'){
                //提现审核通过，写入资金记录表

                Db::name('expense')->insert([
                    'admin_id' => $this->auth->id,
                    'account' => $tixian['account'],
                    'user_id' => $tixian['user_id'],
                    'createtime' => time(),
                    'type' => '1',
                    'note' => '用户提现' . $tixian['account'] . '元'
                ]);


            }

            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }


}
